<?php
session_start();
require_once '../header.php';
function __autoload($className)
	{
    	require_once('../class/' . $className . '.class.php');
	}
session::need_manage();	
if(session::is_manager()==TRUE)
{
if(!empty($_GET))
{
	$Step=intval($_GET["step"]);
	if($Step==1)
	{
		unset($_SESSION["insert"]);
		$ArrSend=$_POST;
		print_r($ArrSend);
		$_SESSION["insert"]['checkerId']=$ArrSend['checker'];
		$DateFormat=$ArrSend['dateformat'];
		$Country=$ArrSend['country'];
		$other=$ArrSend['other'];
		if(intval($ArrSend['price']) != 0)
		{
			$ArrCC=explode("\n",$_POST["ccinfo"]);
			for($i=0;$i<count($ArrCC);$i++)
			{
				$CcInfo=$ArrCC[$i];
				$ArrCcInfo=preg_split("/[\s]*[|][\s]*/",$CcInfo);
				for($idccnum=0;$idccnum<count($ArrCcInfo);$idccnum++)
				{
					
					if(strlen($ArrCcInfo[$idccnum])>14&&(is_numeric($ArrCcInfo[$idccnum])==TRUE))
					{
						$CcNum=rtrim(trim($ArrCcInfo[$idccnum]));
						$ArrSubmit['ccnumber']=$CcNum;
						if(substr($CcNum,0,1)==4)
						{
							$ArrSubmit['typecc']="VISA";
						}
						else if(substr($CcNum,0,1)==5)
						{
							$ArrSubmit['typecc']="MASTER";
						}
						else if(substr($CcNum,0,1)==3)
						{
							$ArrSubmit['typecc']="AMEX";
						}
						else if(substr($CcNum,0,1)==5)
						{
							$ArrSubmit['typecc']="DIS";
						}
						$ArrSubmit['country']=$Country;
						$ArrCCPost[]=$CcNum;
						$ArrSubmit['price']=round($ArrSend['price'],2);
						$ArrSubmit['site']='seller';
						$PosFname=intval($ArrSend['fname'])-1;
						if($PosFname==-1)
						{
							$ArrSubmit['firstname']=null;
						}
						else
						{
							$ArrSubmit['firstname']=$ArrCcInfo[$PosFname];
							$ArrCCPost[]=$ArrCcInfo[$PosFname];
						}
						$PostLname=intval($ArrSend['lname'])-1;
						if($PostLname==-1)
						{
							$ArrSubmit['lastname']=NULL;
						}
						else 
						{
							$ArrSubmit['lastname']=$ArrCcInfo[$PostLname];
							$ArrCCPost[]=$ArrCcInfo[$PostLname];
						}
						$PostExmonth=intval($ArrSend['exmonth'])-1;
						$PostExyear=intval($ArrSend['exyear'])-1;
						if(($PostExmonth==$PostExyear)&&($PostExmonth != -1))
						{
							if($DateFormat==1)
							{
								$ExSplit=rtrim(trim($ArrCcInfo[$PostExmonth]));
								$ArrSubmit['exmonth']=substr($ExSplit, 0,2);
								$ArrSubmit['exyear']=substr($ExSplit, 2,4);
							}
							else if($DateFormat==2) 
							{
								$ExSplit=rtrim(trim($ArrCcInfo[$PostExmonth]));
								$ArrSubmit['exmonth']=substr($ExSplit, 0,2);
								$ArrSubmit['exyear']="20".substr($ExSplit, 2,2);
							}
							else if($DateFormat==3)
							{
								$ExSplit=rtrim(trim($ArrCcInfo[$PostExmonth]));
								$ArrSubmit['exmonth']=substr($ExSplit, 2,2);
								$ArrSubmit['exyear']="20".substr($ExSplit, 0,2);
							}
							else if($DateFormat==4)
							{
								$ExSplit=rtrim(trim($ArrCcInfo[$PostExmonth]));
								$ArrSubmit['exmonth']=substr($ExSplit, 4,2);
								$ArrSubmit['exyear']=substr($ExSplit, 0,4);
							}
						}
						else 
						{
							if($PostExmonth==-1)
							{
								$ArrSubmit['exmonth']=NULL;
							}
							else 
							{
								$ArrSubmit['exmonth']=rtrim(trim($ArrCcInfo[$PostExmonth]));
								$ArrCCPost[]=rtrim(trim($ArrCcInfo[$PostExmonth]));
							}
							if($PostExyear==-1)
							{
								$ArrSubmit['exyear']=NULL;
							}
							else 
							{
								if(strlen($ArrCcInfo[$PostExyear])==2)
								{
									$ArrSubmit['exyear']="20".rtrim(trim($ArrCcInfo[$PostExyear]));
								}
								else 
								{
									$ArrSubmit['exyear']=rtrim(trim($ArrCcInfo[$PostExyear]));
								}
								
								$ArrCCPost[]=rtrim(trim($ArrCcInfo[$PostExyear]));
							}
						}
						$PostCcv=intval($ArrSend['ccv'])-1;
						if($PostCcv==-1)
						{
							$ArrSubmit['ccv2']=null;
						}
						else 
						{
							$ArrSubmit['ccv2']=rtrim(trim($ArrCcInfo[$PostCcv]));
							$ArrCCPost[]=rtrim(trim($ArrCcInfo[$PostCcv]));
						}
						$PostAddress=intval($ArrSend['address'])-1;
						if($PostAddress==-1)
						{
							$ArrSubmit['address']=NULL;
						}
						else 
						{
							$ArrSubmit['address']=$ArrCcInfo[$PostAddress];
							$ArrCCPost[]=$ArrCcInfo[$PostAddress];
						}
						
						$PostCity=intval($ArrSend['city'])-1;
						if($PostCity==-1)
						{
							$ArrSubmit['city']=NULL;
						}
						else 
						{
							$ArrSubmit['city']=$ArrCcInfo[$PostCity];
							$ArrCCPost[]=$ArrCcInfo[$PostCity];
						}
						
						$PostState=intval($ArrSend['state'])-1;
						if($PostState==-1)
						{
							$ArrSubmit['state']=NULL;
						}
						else 
						{
							$ArrSubmit['state']=$ArrCcInfo[$PostState];
							$ArrCCPost[]=$ArrCcInfo[$PostState];
						}
						$PostZip=intval($ArrSend['zip'])-1;
						if($PostZip==-1)
						{
							$ArrSubmit['zip']=NULL;
						}
						else
						{
							$ArrSubmit['zip']=$ArrCcInfo[$PostZip];
							$ArrCCPost[]=$ArrCcInfo[$PostZip];
						}
						
						$PostDob=intval($ArrSend['dob'])-1;
						if($PostDob==-1)
						{
							$ArrSubmit['dob']=null;
						}
						else 
						{
							$ArrSubmit['dob']=$ArrCcInfo[$PostDob];
							$ArrCCPost[]=$ArrCcInfo[$PostDob];
						}
						$PostSsn=intval($ArrSend['ssn'])-1;
						if($PostSsn==-1)
						{
							$ArrSubmit['ssn']=NULL;
						}
						else 
						{
							$ArrSubmit['ssn']=$ArrCcInfo[$PostSsn];
							$ArrCCPost[]=$ArrCcInfo[$PostSsn];
						}
						$ArrAfterDiff=array_diff($ArrCcInfo, $ArrCCPost);
						$other=NULL;
						foreach($ArrAfterDiff as $Others)
						{
							$other=$Others." ".$other;
						}
						$ArrSubmit['others']=$other;
						$ArrSubmit['timeorder']='0';
						$ArrSubmit['checker_id']=$_SESSION["insert"]['checkerId'];
						
					}
					
				}
				$Insert[$i]=$ArrSubmit;
			}
			$_SESSION["insert"]=$Insert;
			redirect::location("addccconfirm.php");
			
			
			
		}
		else 
		{
		
		?>
		<script type="text/javascript" >
		alert("chưa điền giá");
		</script>
		<?php 
		
		}
		
	}
	else if($Step==2)
	{
		$webroot="../";
		require_once '../html/admintemplate.php';
		unset($_SESSION["insert"]);
		$ArrDelete=$_POST["cc"];
		for($i=0;$i<count($ArrDelete);$i++)
		{
			$CcNum=$ArrDelete[$i];
			unset($_SESSION["cc"][$CcNum]);
		}
		$ArrCC=$_SESSION["cc"];
		foreach($ArrCC as $InsertDB)
		{
			$InsertIntoDb=database::Insert('ccinfo', $InsertDB);
			if($InsertIntoDb==TRUE)
			{
				echo "Add To Database ".$InsertDB['ccnumber']."<br/>";
			}
			else
			{
				echo "<i><font color=red>Mistake".$InsertDB['ccnumber']."</font></i><br/>";
			}
		}
	}
	
}
else 
{
	$getElectronicId=buycc::findChecker("electronic");
	$walletId=buycc::findChecker("wallet");
	require_once '../html/addcc.php';
}
}
?>